variables-and-constants
Variables and Constants#
What do we mean by the word variable?
Variable is a place where we can store our data, and we can do that using some special keywords like var, let, and const for constant "variable".
Let's assume that we have a person called Ali and his age is 30 and we want to store his age in our program. As we said, we have the variable which acts as a place to store our data so let's see how we will do that with the different ways of defining variables in JavaScript:
var:
let:
const:
So our steps of storing a value in a variable are:
First, we write down one of the three keywords
var,let, orconst.Then, we define the name of the variable which is
agein our case.๐ก You can name your variable whatever name you want but we need to follow a few rules mentioned below
Then to assign a value to a variable we use the single assignment operator
=.The final step is to assign the value which in this case the number
30to the variableage.
We can also store the value in two steps:
- Define the variable name (declaring step).
- Assign/Update the value
30to the variableage(assigning step).๐ก Because we defined the variable
ageon step 1 so no need to use the keywordvaragain with step 2
The general syntax for creating a variable:
var variableName = value;
๐ก For now, the same thing we apply for defining the variable with
varis also applied tolet, and we will see the difference betweenvarandletin later sessions.
Now, let's see some examples
But what about const?#
We use const to define a constant variable, once we assign a value to the variable then we can't change it later and hence the name const
Example
As we said, it is constant and we can't give it(re-assign) a different value to it, and also we can't declare a variable with const and not giving it a value because if we did that we wouldn't be able to give it a value later since it's constant and therefor the variable would be useless. And so this is the difference between const and the other two (var and let)
When to use var, let, and const?#
Nowadays it is "recommended" to use let and const instead of var(we will see why later). Use let whenever you are planning on changing the value of the variable, and const when you are planning on keeping the value as it is.
Rules for Naming Variables#
JavaScript has only a few rules for variable names:
Variable names are case sensitive:
myNameandmynameare two different variablesVariable name must start with a letter, an underscore(_), or a dollar sign($) and you can't use numbers as the first character.
Variable name must not contain symbols, spaces or any other punctuation marks.
You can't use one of JavaScript's reserved words as a variable name.
Examples
- Also, one of the best practices in naming variables in javascript is to follow the camelCase convention
Examples
Final words about let and const#
let and const were introduced with the release of ECMAScript 2015, commonly known as ES6. So you are highly likely to see old applications that don't contain let and const.
Practice Time ๐จโ๐ป#
Follow the following instructions, then write your code inside the index file.